﻿Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.GamerServices
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Media

''' <summary>
''' This is the main type for your game
''' </summary>
Public Class MyFirstGame
    Inherits Microsoft.Xna.Framework.Game
    Private graphics As GraphicsDeviceManager
    Private spriteBatch As SpriteBatch

    Private mSquareGuy As SquareGuy
    Private mBackground As GameBackground
    Public Const SquareGuyMoveRate As Integer = 3

    Private mWizardSquare As Wizard

    Public Sub New()
        graphics = New GraphicsDeviceManager(Me)
        'graphics.GraphicsProfile = GraphicsProfile.HiDef
        Content.RootDirectory = "Content"
    End Sub

    ''' <summary>
    ''' Allows the game to perform any initialization it needs to before starting to run.
    ''' This is where it can query for any required services and load any non-graphic
    ''' related content.  Calling base.Initialize will enumerate through any components
    ''' and initialize them as well.
    ''' </summary>
    Protected Overrides Sub Initialize()
        ' TODO: Add your initialization logic here
        mBackground = New GameBackground
        mWizardSquare = New Wizard
        mWizardSquare.Scale = 0.5
        MyBase.Initialize()
    End Sub

    ''' <summary>
    ''' LoadContent will be called once per game and is the place to load
    ''' all of your content.
    ''' </summary>
    Protected Overrides Sub LoadContent()
        ' Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = New SpriteBatch(GraphicsDevice)

        ' TODO: use this.Content to load your game content here
        mBackground.Load(Me.Content, GraphicsDevice)
        mWizardSquare.Load(Me.Content, GraphicsDevice)
    End Sub

    ''' <summary>
    ''' UnloadContent will be called once per game and is the place to unload
    ''' all content.
    ''' </summary>
    Protected Overrides Sub UnloadContent()
        ' TODO: Unload any non ContentManager content here
    End Sub


    ''' <summary>
    ''' Allows the game to run logic such as updating the world,
    ''' checking for collisions, gathering input, and playing audio.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Protected Overrides Sub Update(ByVal gameTime As GameTime)
        ' Allows the game to exit
        If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
            Me.[Exit]()
        End If

        ' TODO: Add your update logic here
        '_mNewSquareGuyCounter += gameTime.ElapsedGameTime.TotalSeconds

        'If _mNewSquareGuyCounter > 1 Then
        '    NewSquareGuy()
        '    _mNewSquareGuyCounter = 0
        'End If

        'MoveSquares()
        'mBackground.Update(gameTime, GraphicsDevice, 0)
        mWizardSquare.Update(gameTime, mBackground)

        MyBase.Update(gameTime)
    End Sub

    '#Region "Square Guys"

    '    Private Sub MoveSquares()
    '        For Each oSquare In _mSquareGuys
    '            MoveSquare(oSquare)
    '        Next
    '    End Sub
    '    Private Sub MoveSquare(ByVal square As SquareGuy)
    '        Dim intNewPositionX As Integer
    '        Dim intNewPositionY As Integer

    '        Dim intMaxX As Integer = GraphicsDevice.Viewport.Width - square.SpriteTexture.Width
    '        Dim intMaxY As Integer = GraphicsDevice.Viewport.Height - square.SpriteTexture.Height

    '        Select Case square.Position.X
    '            Case Is <= 0
    '                Select Case square.CurrentMoveMode
    '                    Case BaseSprite.MoveMode.DownLeft
    '                        square.CurrentMoveMode = BaseSprite.MoveMode.DownRight
    '                    Case BaseSprite.MoveMode.UpLeft
    '                        square.CurrentMoveMode = BaseSprite.MoveMode.UpRight
    '                End Select
    '            Case Is >= intMaxX
    '                Select Case square.CurrentMoveMode
    '                    Case BaseSprite.MoveMode.DownRight
    '                        square.CurrentMoveMode = BaseSprite.MoveMode.DownLeft
    '                    Case BaseSprite.MoveMode.UpRight
    '                        square.CurrentMoveMode = BaseSprite.MoveMode.UpLeft
    '                End Select
    '        End Select
    '        Select Case square.Position.Y
    '            Case Is <= 0
    '                Select Case square.CurrentMoveMode
    '                    Case BaseSprite.MoveMode.UpLeft
    '                        square.CurrentMoveMode = BaseSprite.MoveMode.DownLeft
    '                    Case BaseSprite.MoveMode.UpRight
    '                        square.CurrentMoveMode = BaseSprite.MoveMode.DownRight
    '                End Select
    '            Case Is >= intMaxY
    '                Select Case square.CurrentMoveMode
    '                    Case BaseSprite.MoveMode.DownLeft
    '                        square.CurrentMoveMode = BaseSprite.MoveMode.UpLeft
    '                    Case BaseSprite.MoveMode.DownRight
    '                        square.CurrentMoveMode = BaseSprite.MoveMode.UpRight
    '                End Select
    '        End Select

    '        intNewPositionX = square.Position.X
    '        intNewPositionY = square.Position.Y

    '        Select Case square.CurrentMoveMode
    '            Case BaseSprite.MoveMode.UpLeft
    '                intNewPositionX -= SquareGuyMoveRate
    '                intNewPositionY -= SquareGuyMoveRate
    '            Case BaseSprite.MoveMode.UpRight
    '                intNewPositionX += SquareGuyMoveRate
    '                intNewPositionY -= SquareGuyMoveRate
    '            Case BaseSprite.MoveMode.DownLeft
    '                intNewPositionX -= SquareGuyMoveRate
    '                intNewPositionY += SquareGuyMoveRate
    '            Case BaseSprite.MoveMode.DownRight
    '                intNewPositionX += SquareGuyMoveRate
    '                intNewPositionY += SquareGuyMoveRate
    '        End Select
    '        square.MoveTo(intNewPositionX, intNewPositionY)
    '    End Sub

    '    Private _mNewSquareGuyCounter As Double = 0
    '    Private _mSquareGuys As New List(Of SquareGuy)

    '    Private Sub NewSquareGuy()
    '        Dim rndRandom As New Random
    '        Dim intYCoordinate As Integer
    '        Dim intXCoordinate As Integer
    '        Dim sqgSquareGuy As New SquareGuy
    '        sqgSquareGuy.Load(Me.Content)

    '        intYCoordinate = rndRandom.Next(0, GraphicsDevice.Viewport.Height - sqgSquareGuy.SpriteTexture.Height)
    '        intXCoordinate = rndRandom.Next(0, GraphicsDevice.Viewport.Width - sqgSquareGuy.SpriteTexture.Width)

    '        sqgSquareGuy.MoveTo(intXCoordinate, intYCoordinate)
    '        _mSquareGuys.Add(sqgSquareGuy)
    '    End Sub
    '#End Region

    ''' <summary>
    ''' This is called when the game should draw itself.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Protected Overrides Sub Draw(ByVal gameTime As GameTime)
        GraphicsDevice.Clear(Color.CornflowerBlue)

        ' TODO: Add your drawing code here
        spriteBatch.Begin()
        mBackground.Draw(gameTime, Me.spriteBatch)

        'For Each oSquareGuy As SquareGuy In _mSquareGuys
        '    oSquareGuy.Draw(gameTime, Me.spriteBatch)
        'Next

        mWizardSquare.Draw(gameTime, Me.spriteBatch)
        spriteBatch.End()

        MyBase.Draw(gameTime)
    End Sub

End Class
